package someTestExcemple.huaweiOd.dp;
import java.util.*;
public class PlayCardCopy {
    // 使用记忆化搜索优化，避免重复计算
    static Map<String, Integer> memo = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String cards = scanner.nextLine();
        scanner.close();

        // 统计每种牌的数量，下标0不使用，1-13对应1-K
        int[] cardCount = new int[14];

        // 解析输入的牌
        for (int i = 0; i < cards.length(); i++) {
            char c = cards.charAt(i);
            if (c >= '1' && c <= '9') {
                cardCount[c - '0']++;
            } else if (c == '0') {
                cardCount[10]++; // 10用0表示
            } else if (c == 'J') {
                cardCount[11]++;
            } else if (c == 'Q') {
                cardCount[12]++;
            } else if (c == 'K') {
                cardCount[13]++;
            }
        }

        // 计算最高得分
        int result = dfs(cardCount);

        System.out.println(result);
    }

    // 将牌的数量转换为缓存键
    static String getKey(int[] cardCount) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 13; i++) {
            sb.append(cardCount[i]).append(",");
        }
        return sb.toString();
    }

    // 深度优先搜索，计算最高得分
    static int dfs(int[] cardCount) {
        // 检查是否还有牌
        boolean hasCards = false;
        for (int i = 1; i <= 13; i++) {
            if (cardCount[i] > 0) {
                hasCards = true;
                break;
            }
        }

        // 如果没有牌了，返回0分
        if (!hasCards) {
            return 0;
        }

        // 检查是否已经计算过这种牌型
        String key = getKey(cardCount);
        if (memo.containsKey(key)) {
            return memo.get(key);
        }

        int maxScore = 0;

        // 尝试取出炸弹（4张相同的牌）
        for (int i = 1; i <= 13; i++) {
            if (cardCount[i] >= 4) {
                cardCount[i] -= 4;
                maxScore = Math.max(maxScore, i * 4 * 3 + dfs(cardCount));
                cardCount[i] += 4;
            }
        }

        // 尝试取出顺子（5张连续的牌，只考虑1-9之间的顺子）
        for (int i = 1; i <= 9 - 4; i++) {
            if (cardCount[i] > 0 && cardCount[i+1] > 0 && cardCount[i+2] > 0 && cardCount[i+3] > 0 && cardCount[i+4] > 0) {
                cardCount[i]--;
                cardCount[i+1]--;
                cardCount[i+2]--;
                cardCount[i+3]--;
                cardCount[i+4]--;
                int sum = i + (i+1) + (i+2) + (i+3) + (i+4);
                maxScore = Math.max(maxScore, sum * 2 + dfs(cardCount));
                cardCount[i]++;
                cardCount[i+1]++;
                cardCount[i+2]++;
                cardCount[i+3]++;
                cardCount[i+4]++;
            }
        }

        // 尝试取出三张相同的牌
        for (int i = 1; i <= 13; i++) {
            if (cardCount[i] >= 3) {
                cardCount[i] -= 3;
                maxScore = Math.max(maxScore, i * 3 * 2 + dfs(cardCount));
                cardCount[i] += 3;
            }
        }

        // 尝试取出对子（2张相同的牌）
        for (int i = 1; i <= 13; i++) {
            if (cardCount[i] >= 2) {
                cardCount[i] -= 2;
                maxScore = Math.max(maxScore, i * 2 * 2 + dfs(cardCount));
                cardCount[i] += 2;
            }
        }

        // 尝试取出单张牌
        for (int i = 1; i <= 13; i++) {
            if (cardCount[i] >= 1) {
                cardCount[i]--;
                maxScore = Math.max(maxScore, i + dfs(cardCount));
                cardCount[i]++;
            }
        }

        // 记录当前牌型的最高得分
        memo.put(key, maxScore);
        return maxScore;
    }
}
